package jafou.server;


import jafou.Model.Forum;
import jafou.Model.Topic;
import jafou.client.remote.JafouService;

import java.util.HashMap;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class JafouServlet extends RemoteServiceServlet implements JafouService {
	
	@Override
	public String[][] getForums() {
		ForumHandler handler = new ForumHandler();
		Forum[] forums = handler.getAllForums();
		String[][] result = new String[forums.length][2];
		for(int i=0; i<forums.length; i++) {
			result[i][0] = forums[i].getTitle();
			result[i][1] = "" + forums[i].getId();
		}
		return result;
	}

	@Override
	public String[][] getTopicsInForum(String forumId) {
		ForumHandler handler = new ForumHandler();
		return handler.getTopics(forumId);
	}

	@Override
	public HashMap<String, String> getUserAttributes() {
		JafouEnv env = new JafouEnv();
		return env.getUserAttributes();
	}

	@Override
	public String addForum(String title, String tagline) {
		ForumHandler handler = new ForumHandler();
		return handler.addForum(title, tagline);
	}

	@Override
	public String delForum(String id) {
		ForumHandler handler = new ForumHandler();
		handler.delForum(id);
		return "";
	}

	@Override
	public String addTopic(String title, String forumId) {
		TopicsHandler handler = new TopicsHandler();
		return handler.addTopic(forumId, title);
	}

	@Override
	public String[][] getPostsInTopic(String topicId) {
		TopicsHandler handler = new TopicsHandler();
		return handler.getMessages(topicId);
	}

	@Override
	public String getUserIdForTopic(String topicId) {
		TopicsHandler handler = new TopicsHandler();
		Topic topic = handler.getTopic(topicId);
		if(topic != null) {
			return topic.getUserId();
		}
		return "";
	}

	@Override
	public String addPost(String message, String topicId) {
		TopicsHandler handler = new TopicsHandler();
		return handler.postMessage(topicId, message);
	}

	@Override
	public String delPost(String id) {
		TopicsHandler handler = new TopicsHandler();
		handler.delPost(id);
		return null;
	}

	@Override
	public String delTopic(String id) {
		TopicsHandler handler = new TopicsHandler();
		return handler.delTopic(id);
	}

}
